﻿using RimWorld;
using Verse;
using System;
using System.Collections.Generic;
using System.Linq;
using Verse.Sound;

namespace RimZombieGirl
{
    public class IncidentDefExtension_CursedGraveSpawn : DefModExtension
    {
        public ThingDef buildingDef;
        public IntRange count;
        public int minEdgeDistance = 10; // 距离边缘最小距离
    }

    public class IncidentWorker_CursedGraveSpawn : IncidentWorker
    {
        public static bool TryFindRandomNotEdgeCellWith(int minEdgeDistance, Predicate<IntVec3> validator, Map map, out IntVec3 result)
        {
            for (int i = 0; i < 1000; i++)
            {
                result = CellFinder.RandomNotEdgeCell(minEdgeDistance, map);
                if (result.IsValid && validator(result))
                {
                    return true;
                }
            }

            result = IntVec3.Invalid;
            return false;
        }

        protected override bool CanFireNowSub(IncidentParms parms)
        {
            Map map = (Map)parms.target;
            if (map == null) return false;

            IncidentDefExtension_CursedGraveSpawn ext = def.GetModExtension<IncidentDefExtension_CursedGraveSpawn>();
            if (ext == null || ext.buildingDef == null) return false;

            // 检查地图上是否有可生成位置
            IntVec3 cell;
            return TryFindRandomNotEdgeCellWith(ext.minEdgeDistance, c =>
                c.Standable(map) &&
                !c.Fogged(map) &&
                c.GetFirstBuilding(map) == null &&
                map.zoneManager.ZoneAt(c) == null, map, out cell);
        }

        protected override bool TryExecuteWorker(IncidentParms parms)
        {
            Map map = (Map)parms.target;
            IncidentDefExtension_CursedGraveSpawn ext = def.GetModExtension<IncidentDefExtension_CursedGraveSpawn>();
            if (ext == null || ext.buildingDef == null) return false;

            int spawnCount = ext.count.RandomInRange;
            List<Building> spawnedGraves = new List<Building>();

            for (int i = 0; i < spawnCount; i++)
            {
                if (TryFindRandomNotEdgeCellWith(ext.minEdgeDistance, c =>
                    c.Standable(map) &&
                    !c.Fogged(map) &&
                    c.GetFirstBuilding(map) == null &&
                    map.zoneManager.ZoneAt(c) == null, map, out IntVec3 cell))
                {
                    Building grave = (Building)ThingMaker.MakeThing(ext.buildingDef);
                    GenSpawn.Spawn(grave, cell, map, WipeMode.Vanish);
                    spawnedGraves.Add(grave);
                }
            }

            if (spawnedGraves.Count == 0) return false;
            // 播放音效
            SoundDefOfHelper.PlaySoundOnce("CursedGraveSpawn_Sound", spawnedGraves.First().Position, map);

            // Letter 指向生成的坟墓，使用 Building 构造函数
            LookTargets targets = new LookTargets(spawnedGraves);

            SendStandardLetter(
                def.letterLabel,
                def.letterText,
                def.letterDef,
                parms,
                lookTargets: targets
            );

            return true;
        }
        public static class SoundDefOfHelper
        {
            public static void PlaySoundOnce(string soundDefName, IntVec3 pos, Map map)
            {
                SoundDef def = DefDatabase<SoundDef>.GetNamedSilentFail(soundDefName);
                if (def != null)
                {
                    def.PlayOneShot(new TargetInfo(pos, map));
                }
            }
        }

    }
}
